import type { EnvMirrorOptions } from "@/modules/dev-env/mirror/mirror.ts";

/**
 * 下载源配置
 */
export interface MirrorOptions {
  /**
   * ID
   */
  id: string;
  /**
   * 下载源名称
   */
  name: string;
  /**
   * 下载地址前缀
   */
  prefix: string;
  /**
   * 官网地址
   */
  website?: Nil<string>;
}

export type MirrorNames = "apache" | "aliyun" | "tencent" | "tsinghua" | "huaweicloud" | "nanjingu";

/**
 * 镜像站点
 */
export const MirrorWebsites: Record<MirrorNames, MirrorOptions> = {
  aliyun: {
    id: "aliyun",
    name: "阿里云",
    prefix: "https://mirrors.aliyun.com",
    website: "https://mirrors.aliyun.com/",
  },
  tencent: {
    id: "tencent",
    name: "腾讯云",
    prefix: "https://mirrors.cloud.tencent.com",
    website: "https://mirrors.cloud.tencent.com/",
  },
  huaweicloud: {
    id: "huaweicloud",
    name: "华为云",
    prefix: "https://mirrors.huaweicloud.com",
    website: "https://mirrors.huaweicloud.com/",
  },
  tsinghua: {
    id: "tsinghua",
    name: "清华大学",
    prefix: "https://mirrors.tuna.tsinghua.edu.cn",
    website: "https://mirrors.tuna.tsinghua.edu.cn/",
  },
  nanjingu: {
    id: "nanjingu",
    name: "南京大学",
    prefix: "https://mirrors.nju.edu.cn",
    website: "https://mirrors.nju.edu.cn/",
  },
  apache: {
    id: "apache",
    name: "Apache",
    prefix: "https://dlcdn.apache.org",
    website: "https://maven.apache.org/",
  },
};

/**
 * 获取镜像站点配置
 * @param name 镜像站点名称
 * @param mirrorPath 软件工具在镜像站点中的路径
 */
export function getMirrorOptions(name: MirrorNames, mirrorPath: string): EnvMirrorOptions {
  const config = MirrorWebsites[name];
  return {
    id: config.id,
    name: config.name,
    url: `${config.prefix}${mirrorPath}`,
    website: config.website,
  };
}
